package com.zzyl.nursing.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.MemberThreadLocal;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.service.IReservationService;
import com.zzyl.nursing.vo.ApointmentVo;
import com.zzyl.nursing.vo.ReservationTimeAndCountVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author:zhq
 * @Date: 2025/08/21 21:26
 * @Description:
 */
@Service
public class ReservationImpl extends ServiceImpl<ReservationMapper, Reservation>  implements IReservationService {
    @Autowired
    private ReservationMapper reservationMapper;

    @Override
    public Integer getCancelledCount() {
        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        return reservationMapper.getCancelledCount(startTime, endTime);
    }

    @Override
    public List<ReservationTimeAndCountVo> getCountByTime(Long time) {
       return reservationMapper.getCountByTime(time);
    }

    @Override
    public void add(ApointmentVo apointmentVo) {
        Reservation reservation = new Reservation();
        BeanUtil.copyProperties(apointmentVo, reservation);
        reservation.setStatus(0);
        reservation.setCreateTime(LocalDateTime.now());
        Long memberId = MemberThreadLocal.getMemberId();
        reservation.setCreateBy(memberId);
        baseMapper.insert(reservation);
    }

    @Override
    public TableDataInfo<Reservation> getPage(Integer pageNum, Integer pageSize) {

        Page<Reservation> page = new Page<>(pageNum, pageSize);
        lambdaQuery().page(page);
        TableDataInfo<Reservation> table = new TableDataInfo<>();
        table.setTotal(page.getTotal());
        table.setRows(page.getRecords());
        table.setCode(200);
        table.setMsg("请求成功");
        return table;
    }
}
